package day_2022_9_to_12.Day;

import java.util.HashSet;
import java.util.Set;

/**
 * @author haomin
 * @date 2022/09/08 14:25
 **/
public class Day6_6_Num128_LongestConsecutive {
    class Solution {
        public int longestConsecutive(int[] nums) {
            int longest = 0;
            Set<Integer> set = new HashSet<>();
            for(int num : nums) set.add(num);
            for(int num : nums){
                if(set.remove(num)){
                    int len = 1;
                    int cur = num-1;
                    while(set.remove(cur--)) len++;
                    cur = num+1;
                    while(set.remove(cur++)) len++;
                    longest = Math.max(longest, len);
                }
            }
            return longest;
        }
    }
}